package com.yyj.service;

import com.yyj.dao.TPropertyUserFormMapper;
import com.yyj.model.TPropertyUserForm;
import com.yyj.model.TPropertyUserFormExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class UserFormService {

    @Autowired
    TPropertyUserFormMapper userFormMapper;

    @Transactional(rollbackFor = Exception.class)
    public void setUserFormId(String openId , String formId) throws Exception{
        TPropertyUserForm userForm = new TPropertyUserForm();
        userForm.setFormId(formId);
        userForm.setOpenId(openId);
        userForm.setCreateTime(new Date());
        userForm.setFlag(0);
        userFormMapper.insert(userForm);
    }

    public TPropertyUserForm queryUserForm(String openId){
        TPropertyUserForm userForm = null;
        TPropertyUserFormExample userFormExample = new TPropertyUserFormExample();
        TPropertyUserFormExample.Criteria criteria = userFormExample.createCriteria();
        criteria.andOpenIdEqualTo(openId);
        criteria.andFlagEqualTo(0);
        userFormExample.setOrderByClause("create_time");
        List<TPropertyUserForm> userFormList = userFormMapper.selectByExample(userFormExample);
        if(userFormList.size() > 0){
            userForm = userFormList.get(0);
            userForm.setFlag(1);
            userFormMapper.updateByPrimaryKeySelective(userForm);
        }
        return userForm;

    }

}
